package com.xdwanj.dao.impl;

import com.xdwanj.dao.WordDao;
import com.xdwanj.pojo.Word;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class WordDaoImpl implements WordDao {

    @Autowired
    JdbcTemplate jdbcTemplate;

    @Override
    public void save(Word word) {

        jdbcTemplate.update("insert into words values (?,?,?,?)", null, word.getEnglish(), word.getPhoneticSymbol(), word.getChinese());

    }

    @Override
    public List<Word> findWordByEnglish(String english) {
        List<Word> wordList = jdbcTemplate.query("select * from words where english like '%" + english + "%'", new BeanPropertyRowMapper<>(Word.class));
        return wordList;
    }

    @Override
    public List<Word> findAll() {
        List<Word> wordList = jdbcTemplate.query("select * from words", new BeanPropertyRowMapper<Word>(Word.class));

        return wordList;
    }
}
